package jarvis.fs;

import jarvis.fs.analyzer.WordAnalyzer;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.snowball.SnowballAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

/**
 * 
 * Function: The API class
 * @author Jarvis.Guo
 *
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		
		if(args.length<5)
		{
			usage();
			return;
		}
		int n = Integer.MAX_VALUE;
		String sDir = null;
		//list method names to compute and show
		List<String> methodNames = new ArrayList<String>();
		for(int i=0;i<args.length;i++)
		{
			if("-topn".equals(args[i]))
			{
				n = Integer.parseInt(args[++i]);
			}
			else if("-dir".equals(args[i]))
			{
				sDir = args[++i];
			}
			else
			{
				methodNames.add(args[i]);
			}
		}
		
		if(sDir == null||methodNames.isEmpty())
		{
			usage();
			return;
		}
		
		Directory dir = buildDirectory(new File(sDir));
		FeatureSelection fs = null;
		for(String name:methodNames)
		{
			if(name.equals("DF"))
			{
				fs = new DF(dir);
			}
			else if(name.equals("TS"))
			{
				fs = new TS(dir);
			}
			else if(name.equals("EN"))
			{
				fs = new EN(dir);
			}
			else if(name.equals("TC"))
			{
				fs = new TC(dir);
			}
			else
			{
				System.err.println(name+" is not a measure name now.");
				continue;
			}
			List<String> result = fs.getTopNFeature(n);
			System.out.print(name+" : [");
			boolean isFirst = true;
			for(String s:result)
			{
				if(isFirst) isFirst = false;
				else System.out.print(",");
				System.out.print(s);
			}
			System.out.println("]");
			fs.close();
		}
		

	}
	
	private static void usage()
	{
		System.out.println("Usage:jarvis.fs.Main <-topn N> <-dir directory> <measure1> [measure2]....");
	}
	
	/**
	 * Make the lucene directory by the files under dir
	 * make sure they're text file format
	 * @param dir
	 * @return
	 * @throws IOException
	 */
	public static Directory buildDirectory(File dir) throws IOException
	{
		if(!dir.exists() || dir.isFile())
		{
			throw new IOException(dir+" is not a directory");
		}
		File[] files = dir.listFiles(new FileFilter(){

			public boolean accept(File pathname) {
				if (pathname.isFile()) return true;
				return false;
			}});
		RAMDirectory directory = new RAMDirectory();
		//use wordanalyzer , see its declare
		IndexWriter writer = new IndexWriter(directory,new WordAnalyzer(),true);
		for(File file:files)
		{
			Document doc = buildDocument(file);
			writer.addDocument(doc);
		}
		writer.close();
		return directory;
	}
	
	/**
	 * Build one lucene document per file
	 * @param file
	 * @return
	 * @throws IOException
	 */
	private static Document buildDocument(File file) throws IOException
	{
		if(!file.exists() || file.isDirectory())
		{
			throw new IOException(file+" is not a file");
			
		}
		InputStream is = new BufferedInputStream(new FileInputStream(file));
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		StringBuilder all = new StringBuilder();
		String dateline = null;
		while((dateline = br.readLine())!=null)
		{
			all.append(dateline);
			all.append("\n");
		}
		br.close();
		is.close();
		Document doc = new Document();
		doc.add(new Field("content",all.toString(),Store.NO,Index.TOKENIZED));
		return doc;
	}

}
